package vn.vfriends.mcms.admin.servlet;

import java.io.File;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author tuan@vfriends.vn
 */
@WebServlet(name="dynamicImageServlet", urlPatterns="/image")
public class DynamicImageServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String imagePath = req.getParameter("path");
        if(imagePath == null || imagePath.length() == 0) {
                return;
            }
            File imageFile = new File(imagePath);
            ServletOutputStream out = resp.getOutputStream();
            out.write(IOUtils.toByteArray(FileUtils.openInputStream(imageFile)));
            out.close();
    }
}
